package com.qipay.sp.excutor.shanhongyun;

import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.JsonUtils;
import com.qipay.utils.SignSortMD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ShyunTradeQueryExcutor extends AbstractExcutor {

    private final static String KEY_NAME="key";
    private final static String CONFIG_KEY_NAME="key";
    private final static String CONFIG_APPID_NAME="app_id";
    private final static String SIGN_NAME="sign";

    private final static String API_PARAM_APP_ID="app_id";
    private final static String API_PARAM_OUT_TRADE_NO="order_id";

    private final static String API_URL="https://api.shanhongyun.com/payment/query";

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.WX_TRADE_QUERY;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put(API_PARAM_OUT_TRADE_NO, order.getTradeNo());

        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        Object key = getVal(spconfig, ShyunTradeQueryExcutor.CONFIG_KEY_NAME);
        Object appId = getVal(spconfig, CONFIG_APPID_NAME);

        params.put(API_PARAM_APP_ID, appId);

        String sign = SignSortMD5Utils.getSign(params,
                key.toString(), SIGN_NAME, KEY_NAME);

        params.put(SIGN_NAME, sign);

        String result = sendPostByKeyValue(params, ShyunTradeQueryExcutor.API_URL);
        if(StringUtils.isBlank(result)){
            throw new Exception("请求shanhongyun的查询接口失败");
        }
        Map<String, Object> responseMap = JsonUtils.toMap(result);
        Map<String, Object> resultMap = new HashMap<>();

        if(responseMap == null || !responseMap.containsKey("content") ||
                responseMap.get("content") == null)
        {
            logger.error("请求shanhongyun的查询接口失败:" + result);
            return null;
        }

//        if(!isResponseValid(responseMap, key.toString(), SIGN_NAME, KEY_NAME)) {
//            logger.error("请求shanhongyun的查询返回值的签名失败:" + result);
//            return null;
//        }

        Map<String, Object> contentMap = (Map<String, Object>) responseMap.get("content");

        if(contentMap == null || !contentMap.containsKey("status") ||
                contentMap.get("status") == null)
        {
            logger.error("请求shanhongyun的查询接口失败:" + result);
            return null;
        }

        resultMap.put("result_code", "0");

        resultMap.put("trade_state", "9".equals(contentMap.get("status"))? "SUCCESS":"NOTPAY");
        resultMap.put("trade_no", contentMap.get("order_id"));
        resultMap.put("out_transaction_id", contentMap.get("query_id"));

        return resultMap;
    }
}
